package org.particlethink.utils;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathUtil {
//    public static String join(String... paths) {
//
//    }

    public static String mergeFilePaths(String... paths) {
        Path result = Paths.get(paths[0]);

        for (int i = 1; i < paths.length; i++) {
            result = result.resolve(paths[i]);
        }

        return result.toString();
    }

    public static String mergeUrlPaths(String... urls) throws URISyntaxException {
        StringBuilder mergedUrl = new StringBuilder();
        boolean first = true; // 标记是否是第一个URL片段

        for (String url : urls) {
            if (first) {
                // 去掉第一个URL片段末尾的斜杠
                mergedUrl.append(url.replaceAll("/+$", ""));
                first = false;
            } else {
                // 去掉非第一个URL片段开头的斜杠，并添加一个斜杠
                mergedUrl.append("/").append(url.replaceAll("^/+", ""));
            }
        }

        return mergedUrl.toString();
    }

}
